package com.example.demo.repository.repositoryImpl;

import com.example.demo.pojo.QFile;
import com.example.demo.pojo.QTransaction;
import com.example.demo.pojo.QUser;
import com.example.demo.pojo.User;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Repository
public class UserRepositoryImpl {

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

//    @Autowired
//    private EntityManager entityManager;

//    @Autowired
//    private Pagination pagination;

    public List<Tuple> findUserInfoWithFile() {
        QUser qUser = QUser.user;
        QFile qFile = QFile.file;

        JPAQuery<Tuple> jpaQuery = jpaQueryFactory
                .select(qUser.u_name, qFile.fileName, qUser.u_city)
                .from(qUser)
                .leftJoin(qFile)
                .on(qUser.u_city.eq(qFile.area))
                .where(qUser.isDelete.eq(false)
                        .and(qFile.isDelete.eq(0)));
        return jpaQuery.fetch();
    }

    public Integer countUserInfoWithFile() {
        QUser qUser = QUser.user;
        QFile qFile = QFile.file;

        JPAQuery<Long> jpaQuery = jpaQueryFactory
                .select(qFile.count())
                .from(qUser)
                .leftJoin(qFile)
                .on(qUser.u_city.eq(qFile.area))
                .where(qUser.isDelete.eq(false)
                .and(qFile.isDelete.eq(0)));
        return Math.toIntExact(jpaQuery.fetchCount());
    }

    public List<Tuple> findActiveUser(int start, int listNum) {
        QUser qUser = QUser.user;
        QTransaction qTransaction = QTransaction.transaction;
        Calendar calendar = new GregorianCalendar(2019, Calendar.JUNE, 1);
        Date date = calendar.getTime();

        JPAQuery<Tuple> jpaQuery = jpaQueryFactory
                .selectDistinct(qUser.u_id, qUser.u_name, qUser.u_asset, qTransaction.t_time, qTransaction.t_amount)
                .from(qUser)
                .innerJoin(qTransaction)
                .on(qUser.u_id.eq(qTransaction.t_user.u_id))
                .where(qUser.isDelete.eq(false)
                .and(qTransaction.t_time.gt(date)))
                .limit(listNum)
                .offset(start);
        return jpaQuery.fetch();
    }

    public List<Tuple> findActiveUser() {
        QUser qUser = QUser.user;
        QTransaction qTransaction = QTransaction.transaction;
        Calendar calendar = new GregorianCalendar(2019, Calendar.JUNE, 1);
        Date date = calendar.getTime();

//        jpaQueryFactory.select(qUser.u_id, qUser.u_name, qTransaction.t_time)
//                .from(qUser)
//                .innerJoin(qTransaction)
        JPAQuery<Tuple> jpaQuery = jpaQueryFactory
                .selectDistinct(qUser.u_id, qUser.u_name, qUser.u_asset, qTransaction.t_time, qTransaction.t_amount)
                .from(qUser)
                .innerJoin(qTransaction)
                .on(qUser.u_id.eq(qTransaction.t_user.u_id))
                .where(qUser.isDelete.eq(false)
                        .and(qTransaction.t_time.gt(date)));
        return jpaQuery.fetch();
    }

    public Integer countAll(String keyword) {

        QUser qUser = QUser.user;

        JPAQuery<Long> jpaQuery = jpaQueryFactory
                .select(qUser.count())
                .from(qUser)
                .where(qUser.u_name.like("%" + keyword + "%")
                .and(qUser.isDelete.eq(false)));
         return Math.toIntExact(jpaQuery.fetchCount());
    }

    public List<User> findByPage(int start, int listNum, String keywords) {
        QUser qUser = QUser.user;

        JPAQuery<User> jpaQuery = jpaQueryFactory
                .selectFrom(qUser)
                .where(qUser.isDelete.eq(false)
                .and(qUser.u_name.like("%" + keywords + "%")))
                .limit(listNum)
                .offset(start);
        return jpaQuery.fetch();
    }

//    public Page<User> findByPage2(int pageNum, int listNum, String keywords) {
//        QueryHelper helper = new QueryHelper("select u.*", "user u");
//        helper.addCondition("u.is_delete = false");
//        helper.addCondition("u.u_name like ?", "%" + keywords + "%");
////        return pagination.find(helper.getSql(), User.class);
//        return pagination.paginate(helper.getSql(), User.class, pageNum, listNum);
//    }
}
